package com.aaron.kjviewutils.driving;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.glide.GlideCircleTransform;
import com.aaron.kjviewutils.glide.WebpDataModel;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/3.
 */

public class DrivingDocAdapter extends RecyclerView.Adapter<DrivingDocAdapter.DrivingDocViewHolder> {

    private List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> datas;
    private Context context;

    private OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public DrivingDocAdapter(Context context, List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> datas)
    {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public DrivingDocViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_driving_doc, parent, false);
        return new DrivingDocViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final DrivingDocViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position))
        {
            DrivingDocBean.DrivingDocInfo.DrivingDocInner drivingDocInner = datas.get(position);
            if("judge".equals(drivingDocInner.tikuType))
            {
                //判断题
                Glide.with(context).load(new WebpDataModel("")).transform(new GlideCircleTransform(context)).centerCrop().dontAnimate().placeholder(R.mipmap.ic_launcher).into(holder.tikuType);
            }
            else if("select".equals(drivingDocInner.tikuType))
            {
                //选择题
                Glide.with(context).load(new WebpDataModel("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=678593932,1869610318&fm=23&gp=0.jpg")).transform(new GlideCircleTransform(context)).centerCrop().dontAnimate().placeholder(R.mipmap.ic_launcher).into(holder.tikuType);
            }
            holder.tikuId.setText(drivingDocInner.id);
            holder.tikuTitle.setText(drivingDocInner.title);
            if(null!=mOnItemClickListener)
            {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }


    public static class DrivingDocViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.tikuType)
        ImageView tikuType;
        @Bind(R.id.tikuId)
        TextView tikuId;
        @Bind(R.id.tikuTitle)
        TextView tikuTitle;

        public DrivingDocViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
